#include"TcpServer.hpp"
#include"Protocols.hpp"

// std::string func1(const std::string& package)
// {
//     std::string ret_str(package);
//     ret_str+="to client\n";
//     return ret_str;
// }

std::string CaCulate(std::string &date)
{
    std::string send_date_str = "";
    std::string content;
    Request req;
    Response res;
    if (!Deheader(date,content) || !req.Deserialize(content))//因先识别报头再进行解析
    {
        res.code_ = ResCode::Lose;
        return "";
    }
    else
    {

        switch (req.op_)
        {
        case '+':
            res.results_ = req.x_ + req.y_;
            break;
        case '-':
            res.results_ = req.x_ - req.y_;
            break;
        case '*':
            res.results_ = req.x_ * req.y_;
            break;
        case '/':
            if (0 == req.y_)
            {
                res.code_ = ResCode::ZeroDivision;
                break;
            }
            res.results_ = req.x_ / req.y_;
            break;
        case '%':
            if(0==req.y_)
            {
                res.code_=ResCode::ZeroMode;
                break;
            }
        default:
            res.code_ = ResCode::InvalidOperator;
            break;
        }
    }
    res.Serialize(&send_date_str);//因先变成字符串在添加报头
    Header(&send_date_str);
    return send_date_str;
}

int main()
{
    TcpServer server;
    server.InIt();
    server.Run(CaCulate);
}